/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.ArrayList;
import java.util.List;
import org.esgi.model.impl.Form;
import org.esgi.model.interfaces.IFormInfo;

/**
 *
 * @author robertmogos
 */
public class FormListView extends javax.swing.JPanel {

    /**
     * Creates new form FormListView
     */
    public FormListView() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        formJList = new javax.swing.JList();
        nextButton = new javax.swing.JButton();
        titleLabel = new javax.swing.JLabel();
        questionLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(740, 520));
        setSize(new java.awt.Dimension(740, 520));

        formJList.setFont(new java.awt.Font("Helvetica", 0, 36)); // NOI18N
        formJList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        formJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                formJListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(formJList);

        nextButton.setText("Valider >>");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        titleLabel.setFont(new java.awt.Font("Helvetica", 0, 24)); // NOI18N

        questionLabel.setFont(new java.awt.Font("Helvetica", 0, 18)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Helvetica", 0, 36)); // NOI18N
        jLabel1.setText("Choisissez un formulaire");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 355, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 46, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(titleLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 315, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(questionLabel))
                        .add(18, 18, 18))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(nextButton)
                                .addContainerGap())
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel1)
                                .add(249, 249, 249))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(42, 42, 42)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 54, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 289, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(85, 85, 85)
                        .add(titleLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(38, 38, 38)
                        .add(questionLabel)))
                .add(42, 42, 42)
                .add(nextButton)
                .add(27, 27, 27))
        );

        getAccessibleContext().setAccessibleName("");
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        if(-1 != selectedIndex){
            if(null != selectionListener){
                selectionListener.formSelected(forms.get(selectedIndex));
            }
        }
    }//GEN-LAST:event_nextButtonActionPerformed

    private void formJListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_formJListValueChanged
        selectedIndex = formJList.getSelectedIndex();
        
        IFormInfo f = forms.get(selectedIndex);
        questionLabel.setText("Nombre de questions: " + f.getQuestionsCount());
        titleLabel.setText(f.getName());
    }//GEN-LAST:event_formJListValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList formJList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JLabel questionLabel;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
    
    private List<IFormInfo> forms;
    private Integer selectedIndex = -1;
    private FormListListener selectionListener;
    
    public void addSelectionListener(FormListListener listener)
    {
        selectionListener = listener;
    }
    
    public void setForms(List<IFormInfo> f)
    {
        forms = f;
        List<String> items = new ArrayList<String>();
        for(IFormInfo form : forms){
            items.add(form.getName());
        }
        formJList.setListData(items.toArray());
    }   
}
